import Vue from 'vue'

let setHistory = (subject) => {
    let histories = getHistories();
    for(let idx=0,ln=histories.length;idx<ln;idx++) {
        let history = histories[idx];
        if(history && history.id && history.id===subject.id) {
            histories.splice(idx, 1);
        }
    }
    if(histories.length>10){
        histories.splice(histories.length-1, 1);
    }
    histories.splice(0, 0, subject);
    let str = JSON.stringify(histories);
    localStorage.setItem("game-histories", str)
};

let getHistories = ()=> {
    let histories = [];
    let str = localStorage.getItem("game-histories");
    if(str) {
        histories = JSON.parse(str);
    }
    return histories
};

Vue.prototype.gameHistory = {
    setHistory: setHistory,
    getHistory: getHistories
};